<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['user:userInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="头像" align="center" prop="avatar" #default="scope">
        <img :src="scope.row.avatar" width="50">
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="电话号码" align="center" prop="phone" />
      <el-table-column label="状态" align="center" >
        <span v-if="status==1">正常</span>
        <span v-else>停用</span>
      </el-table-column>
      <el-table-column label="最后一次登录时间" align="center" prop="lastLoginTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="getDetail(scope.row)"
                     v-hasPermi="['user:userInfo:edit']">详情</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改会员对话框 -->
    <el-dialog :title="title" v-model="open" width="1100px" append-to-body>
      <el-form ref="userInfoRef" :model="form" :rules="rules" label-width="80px" >
        <hr>
        <el-form-item label="基本信息" >
          <div style="width: 500px;">
            <el-form-item label="头像" prop="avatar" label-width="120px">
              <img :src="form.avatar" width="50">
            </el-form-item>
            <el-form-item label="昵称" prop="nickName" label-width="120px">
              {{form.nickName}}
            </el-form-item>
            <el-form-item label="电话号码" prop="phone" label-width="120px">
              {{form.phone}}
            </el-form-item>
            <el-form-item label="最后一次登录ip" prop="lastLoginIp" label-width="120px">
              {{form.lastLoginIp}}
            </el-form-item>
            <el-form-item label="状态" prop="lastLoginIp" label-width="120px">
              <span v-if="form.status==1">正常</span>
              <span v-else>停用</span>
            </el-form-item>
          </div>
          <div>
            <el-form-item label="用户名" prop="username" label-width="130px">
              {{form.username}}
            </el-form-item>
            <el-form-item label="用户名" prop="sex" label-width="130px">
              <span v-if="form.sex==1">男</span>
              <span v-else>女</span>
            </el-form-item>
            <el-form-item label="备注" prop="memo" label-width="130px">
              {{form.memo}}
            </el-form-item>
            <el-form-item label="最后一次登录时间" prop="memo" label-width="130px">
                <span>{{ parseTime(form.lastLoginTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
            </el-form-item>
            <el-form-item label="创建时间" prop="memo" label-width="130px">
              <span>{{ parseTime(form.createTime, '{y}-{m}-{d} {h}:{m}:{s}') }}</span>
            </el-form-item>

          </div>
        </el-form-item>
        <hr>
        <el-form-item label="地址信息">
          <el-table :data="userAddress" style="width: 100%">
            <el-table-column prop="tagName" label="地址标签" width="80" />
            <el-table-column prop="isDefault" label="是否默认" width="80" />
            <el-table-column prop="fullAddress" label="地址详情" width="600"/>
            <el-table-column prop="createTime" label="创建时间" width="150"/>
          </el-table>
        </el-form-item>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open = false">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="UserInfo">
import { listUserInfo, getUserInfo, addUserInfo, updateUserInfo } from "@/api/user/userInfo";
import {listAllUserAddress} from "../../../api/user/userAddress.js";

const { proxy } = getCurrentInstance();

const userInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    username: null,
    password: null,
    nickName: null,
    phone: null,
    avatar: null,
    sex: null,
    memo: null,
    openId: null,
    unionId: null,
    lastLoginIp: null,
    lastLoginTime: null,
    status: null,
  },
  rules: {
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ],
    delFlag: [
      { required: true, message: "删除标志不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询会员列表 */
function getList() {
  loading.value = true;
  listUserInfo(queryParams.value).then(response => {
    userInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    username: null,
    password: null,
    nickName: null,
    phone: null,
    avatar: null,
    sex: null,
    memo: null,
    openId: null,
    unionId: null,
    lastLoginIp: null,
    lastLoginTime: null,
    status: null,
    createTime: null,
    createBy: null,
    updateTime: null,
    updateBy: null,
    delFlag: null,
    remark: null
  };
  proxy.resetForm("userInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}




/** 提交按钮 */
function submitForm() {
  proxy.$refs["userInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateUserInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addUserInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}


/** 导出按钮操作 */
function handleExport() {
  proxy.download('user/userInfo/export', {
    ...queryParams.value
  }, `userInfo_${new Date().getTime()}.xlsx`)
}


const userAddress = ref([])
/**获取用户详情*/
function getDetail(row){
  open.value = true;
  //获取用户详情
  getUserInfo(row.id).then(response => {
    form.value = response.data
  })
  //获取用户地址信息
  listAllUserAddress().then(response => {
    userAddress.value = response.rows

  })
}
getList();
</script>
